package com.deco.back.controller.store;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;



import com.deco.back.utils.ExcelUtil;
import com.deco.domain.vo.store.StoreJoinVo;
import com.deco.domain.vo.sys.ChinastatesVo;
import com.deco.service.store.StoreJoinService;
import com.deco.service.sys.ChinastatesService;
import com.free.common.vo.PageResponseVo;
import com.free.common.vo.PageSearchVo;
import com.free.common.vo.ResultModel;
import com.free.ext.web.spring.annoation.PageSearch;

/**
 * 广告管理 Controller
 * @author lengy
 *
 */
@Controller
public class StoreJoinController {
	
    private final static String[] EXCEL_TITLES = { "序号", "姓名", "手机号", "省份", "城市", "邮箱", "公司名称", "提交时间","备注" };
	
    @Autowired
    private StoreJoinService storeJoinService;
    
    @Autowired
    private ChinastatesService chinastatesService;
    
    
	
	 /**
    * View视图
    */
   @RequestMapping(value="/store_join_list")
   public ModelAndView storeJoinList() {
       ModelAndView modelAndView = new ModelAndView();
       modelAndView.setViewName("store/storejoinlist");
       Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
       modelAndView.addObject("areaMap", chinastatesMap);
       
       return modelAndView;
   }
   
   @RequestMapping(value= "/store_join_list_page")
   @ResponseBody
   public PageResponseVo<StoreJoinVo> getList(@PageSearch PageSearchVo<StoreJoinVo> condition){
	   PageResponseVo<StoreJoinVo> storeJoinVoPageInfo = storeJoinService.pageInfo(condition);
	   List<StoreJoinVo> storeJoinVoList =  storeJoinVoPageInfo.getList();
	   
	   if(storeJoinVoList != null && storeJoinVoList.size() > 0){
		   Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
		   for(StoreJoinVo storeJoin : storeJoinVoList){
			   ChinastatesVo p = chinastatesMap.get(storeJoin.getProv());
			   if(p != null){
				   storeJoin.setProvName(p.getName());
				   ChinastatesVo c = p.getChildMap().get(storeJoin.getCity());
				   if(c != null){
					   storeJoin.setCityName(c.getName());
				   }
				   else{
					   storeJoin.setCityName("");
				   }
			   }
			   else{
				   storeJoin.setProvName("");
				   storeJoin.setCityName("");
			   }
			  
		   }
	   }
	   return storeJoinVoPageInfo;
   }
   
   @RequestMapping(value = "/store_join_edit.do")
   public ModelAndView edit(Integer id){
	   StoreJoinVo vo = null;
       ModelAndView mv = new ModelAndView("/store/storejoinedit");
       if(id != null){
           vo = storeJoinService.getStoreJoin(id);
           mv.addObject("actNm","更新");
       }else{
           vo = new StoreJoinVo();
           vo.setId(0L);
           mv.addObject("actNm","新增");
       }
       mv.addObject("obj",vo);
       Map<String,ChinastatesVo> chinastatesMap =  chinastatesService.getAll();
       mv.addObject("areaMap", chinastatesMap);
       
       return mv;
   }
    
   
   @RequestMapping(value ="/store_join_edit_submit.do")
   @ResponseBody
   public ResultModel udpate(StoreJoinVo vo){
       return storeJoinService.save(vo);
   }

    
    @RequestMapping(value ="/store_join_del.do")
    @ResponseBody
    public ResultModel batchDelete(@RequestParam(value = "ids[]")Integer[] ids){
        return storeJoinService.batchDelete(ids);
    }
    
    @RequestMapping(value = "/store_join_export")
    public void getBankCardList(HttpServletResponse response, StoreJoinVo storeJoin) throws Exception {
        try {
            Map<String, ChinastatesVo> areaMap = chinastatesService.getAll();
            List<StoreJoinVo> result = storeJoinService.selectByNameLike(storeJoin);
            List<List<Object>> recordList = new ArrayList<List<Object>>();
            List<Object> record = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            for (int i = 0; i < result.size(); i++) {
                StoreJoinVo join = result.get(i);
                record = new ArrayList<Object>();
                record.add(Long.valueOf(i + 1));
                record.add(join.getName());
                record.add(join.getPhone());
                ChinastatesVo p = areaMap.get(join.getProv());
                if(p != null){
                    record.add(p.getName());
                    Map<String, ChinastatesVo> city = p.getChildMap();
                    ChinastatesVo c = city.get(join.getCity());
                    record.add(c == null ? "" : c.getName());
                } else {
                    record.add("");
                    record.add("");
                }
                record.add(join.getEmail());
                record.add(join.getCompanyName() == null ? "" : join.getCompanyName()); 
                if(join.getApplyTime() != null){
                    record.add(sdf.format(join.getApplyTime()));
                } else {
                    record.add("");
                }                              
                record.add(join.getRemark() == null ? "" : join.getRemark());
                recordList.add(record);
            }
            ExcelUtil.exportExcel("经销商加盟.xlsx", "经销商加盟数据", EXCEL_TITLES, recordList, response);
        } catch (Exception e) {
            //LOG.error(e.getMessage());
        }
    }
}
